//
//  LYYTabBarViewController.m
//  LYYWeibo
//
//  Created by lanou3g on 15/10/10.
//  Copyright (c) 2015年 李岩岩. All rights reserved.
//

#import "LYYTabBarViewController.h"

#import "HomeViewController.h"
#import "FootmarkViewController.h"
#import "MeViewController.h"
#import "MapViewController.h"
#import "PublishViewController.h"
#import "LYYNavigationController.h"
#import "LYYTabBar.h"

#import <AVOSCloud.h>


@interface LYYTabBarViewController ()<LYYTabBarDelegate>
/**
 *自定义的tabbar
 */
@property (nonatomic, weak) LYYTabBar *customTabBar;

@end

@implementation LYYTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化tabbar
    [self setupTabbar];
    //初始化所有的自控制器
    [self setupAllChildViewControllers];
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}
/**
 *  初始化tabbar
 */
- (void)setupTabbar
{
    LYYTabBar *customTabBar = [[LYYTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}

#pragma mark --tabBar代理方法
/**
 *  监听tabbar按钮的改变
 *  @param from   原来选中的位置
 *  @param to     最新选中的位置
 */
- (void)tabBar:(LYYTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
}
/**
 *  监听加号按钮点击
 */
- (void)tabBardidClickedPlusButton:(LYYTabBar *)tabBar{

    
    //判断是否登录
    AVUser * currentUser = [AVUser currentUser];
    if (currentUser != nil ) {
        //登录中
        PublishViewController *pVC =[[PublishViewController alloc] init];
        NSLog(@"123123123");
        LYYNavigationController *LNVC = [[LYYNavigationController alloc] initWithRootViewController:pVC];
        [self presentViewController:LNVC animated:YES completion:^{
            
        }];
        
    }else{
        //未登录
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您未进行登录,请先登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}
/*
 *初始化所有的自控制器
 */

- (void)setupAllChildViewControllers{
    //1.首页
    HomeViewController *home = [[HomeViewController alloc]init];
//    self.tabBarItem.badgeValue = @"222";
    [self setupChildViewController:home title:@"首页" imageName:@"i_home" selectedImageName:@"home"];
  
    
    //2.足迹
    FootmarkViewController *discover = [[FootmarkViewController alloc]init];
    [self setupChildViewController:discover title:@"足迹" imageName:@"i_footprint" selectedImageName:@"footprint"];
    
    //3.地图
    MapViewController *message = [[MapViewController alloc]init];
    [self setupChildViewController:message title:@"地图" imageName:@"i_navigation" selectedImageName:@"navigation"];
    
    
    //4.我的
    MeViewController *me = [[MeViewController alloc]init];
    [self setupChildViewController:me title:@"我的" imageName:@"i_me" selectedImageName:@"me"];
}

/**
 *初始化一个自控制器
 */

- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
     // 1.设置控制器的属性
    childVc.title = title;
     // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    childVc.tabBarItem.selectedImage = [ [UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 2.包装一个导航控制器
    LYYNavigationController *nav = [[LYYNavigationController alloc]initWithRootViewController:childVc];
    
    [self addChildViewController:nav];
     // 3.添加tabbar内部的按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
    
    
     
}
@end
